package com.wdd.libbase.util

import android.content.Context
import android.graphics.drawable.Drawable
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.bitmap.RoundedCorners
import com.bumptech.glide.request.RequestOptions
import com.wdd.libbase.R
import com.wdd.libbase.http.GlideApp

/**
 * @ProjectName: AndroidExploit
 * @Package: com.android.toolexploit.utils
 * @ClassName: GlideUtils
 * @Description: java类作用描述
 * @Author: 王丹丹
 * @CreateDate: 2019/1/23 0:26
 * @UpdateUser: 更新者
 * @UpdateDate: 2019/1/23 0:26
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
class GlideUtils private constructor() {
    /**
     * 普通加载
     *
     * @param context
     * @param url
     * @param imageView
     */
    fun loadImage(
        context: Context?,
        url: String?,
        imageView: ImageView?
    ) {
        val options = RequestOptions()
            .centerCrop()
            .placeholder(R.drawable.leak_canary_icon)
            .error(R.drawable.leak_canary_icon)
        GlideApp.with(context!!).load(url).apply(options).into(imageView!!)
    }

    /**
     * 加载圆形图片
     *
     * @param context
     * @param url
     * @param imageView
     */
    fun loadCircleImage(
        context: Context?,
        url: String?,
        imageView: ImageView?
    ) {
        val options = RequestOptions()
            .circleCrop()
            .placeholder(R.drawable.leak_canary_icon)
            .error(R.drawable.leak_canary_icon)
        GlideApp.with(context!!).load(url).apply(options).into(imageView!!)
    }

    /**
     * 加载圆角图片
     *
     * @param context
     * @param url
     * @param imageView
     */
    fun loadRoundImage(
        context: Context?,
        url: String?,
        imageView: ImageView?
    ) {
        val options = RequestOptions
            .bitmapTransform(RoundedCorners(20))
            .placeholder(R.drawable.leak_canary_icon)
            .error(R.drawable.leak_canary_icon)
        GlideApp.with(context!!).load(url).apply(options).into(imageView!!)
    }

    /**
     * 展示图片
     *
     * @param context
     * @param path
     * @param imageView
     */
    fun displayImage(
        context: Context?,
        path: Any?,
        imageView: ImageView?
    ) {
        Glide.with(context!!)
            .load(path)
            .into(imageView!!)
    }

    /**
     * 展示图片
     *
     * @param context
     * @param path
     * @param imageView
     * @param width
     * @param height
     * @param placeholder
     * @param strategy
     */
    fun displayImage(
        context: Context?,
        path: Any?,
        imageView: ImageView?,
        width: Int,
        height: Int,
        placeholder: Drawable?,
        strategy: DiskCacheStrategy?
    ) {
        val options = RequestOptions()
            .centerCrop()
            .override(width, height)
            .placeholder(placeholder)
            .diskCacheStrategy(strategy!!)
        Glide.with(context!!)
            .load(path)
            .apply(options)
            .into(imageView!!)
    }

    /**
     * 展示图片
     *
     * @param context
     * @param path
     * @param imageView
     * @param placeholder
     * @param strategy
     */
    fun displayImage(
        context: Context?,
        path: Any?,
        imageView: ImageView?,
        placeholder: Drawable?,
        strategy: DiskCacheStrategy?
    ) {
        val options = RequestOptions()
            .centerCrop()
            .placeholder(placeholder)
            .diskCacheStrategy(strategy!!)
        Glide.with(context!!)
            .load(path)
            .apply(options)
            .into(imageView!!)
    }

    companion object {
        var instance: GlideUtils? = null
            get() {
                synchronized(GlideUtils::class.java) {
                    if (null == field) {
                        field = GlideUtils()
                    }
                }
                return field
            }
    }
}